﻿Imports BUS
Imports DTO
Imports System.Net.Mime.MediaTypeNames
Imports Microsoft.Office.Interop
Imports System.Data.SqlClient

Public Class frmHOADON
    Dim hoadon As New HoaDonDTO()
    Dim hdbus As New HoaDonBUS()


    Private Sub btt_Thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub Clear()
        txt_dongia.Text = ""
        txt_soluong.Text = ""
        cbo_TenNV.Text = ""
        cbo_tenmh.Text = ""
    End Sub

    Private Sub btn_Them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Them.Click
        Dim hd As New HoaDonDTO
        If txt_dongia.Text <> "" And txt_soluong.Text <> "" Then
            Dim tongtien As Integer = Integer.Parse(txt_dongia.Text) * Integer.Parse(txt_soluong.Text)
            Dim cbb As Integer = Convert.GetTypeCode(cbo_tenmh.SelectedValue)
            hd.MaSP = cbb
            hd.NgayMua = dtp_Ngaymua.Value()
            hd.SoLuong = txt_soluong.Text()
            hd.MaNV = cbo_TenNV.SelectedValue()
            hd.TongTien = tongtien
            Label2.Text = tongtien
            hd.DonGia = txt_dongia.Text()
            HoaDonBUS.ThemHD(hd)
            loadHD()
            MessageBox.Show("Đã Thêm Thành Công!", "Thông Báo")
        Else
            MessageBox.Show("Xin Điền Đầy Đủ Thông Tin")
        End If

    End Sub

    Private Sub loadCbb()

        cbo_tenmh.DataSource = MatHangBUS.LayTatCaTT()
        cbo_tenmh.DisplayMember = "TenSP"
        cbo_tenmh.ValueMember = "MaSP"
    End Sub
    Private Sub loadCBBNV()
        cbo_TenNV.DataSource = NhanVienBUS.LAYTATCATT()
        cbo_TenNV.DisplayMember = "TenNV"
        cbo_TenNV.ValueMember = "MaNV"
    End Sub


    Private Sub frmHOADON_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   
        loadHD()
        loadCbb()
        loadCBBNV()
    End Sub

    Private Sub loadHD()
        DataGridView1.DataSource = HoaDonBUS.loadDS()
    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub ComboBoxEx1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btn_Nhaplai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Nhaplai.Click

    End Sub

    

    Private Sub GroupPanel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupPanel1.Click

    End Sub

    Private Sub btn_Xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Xoa.Click
        Dim dil = MessageBox.Show("Bạn Có Thật Sự Muốn Xóa Hóa Đơn Này?", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dil = DialogResult.Yes Then
            Dim hd As New HoaDonDTO()
            hd.MaHD = DataGridView1.Rows(index).Cells(0).Value
            HoaDonBUS.XOA(hd.MaHD)
            loadHD()
            MessageBox.Show("Da Xoa Thanh Cong!")
        End If
       
    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Dim index As Integer
        index = e.RowIndex
        txt_dongia.Text = DataGridView1.Rows(index).Cells(5).Value.ToString()
        txt_soluong.Text = DataGridView1.Rows(index).Cells(4).Value.ToString()
        dtp_Ngaymua.Text = DataGridView1.Rows(index).Cells(2).Value.ToString()
        cbo_tenmh.Text = DataGridView1.Rows(index).Cells(1).Value.ToString()
        cbo_TenNV.Text = DataGridView1.Rows(index).Cells(3).Value.ToString()
    End Sub
    Dim index As Integer
    Private Sub DataGridView1_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        index = e.RowIndex
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        Me.Close()
    End Sub
End Class